<?php
class PaginglinksHelper extends AppHelper {
    var $helpers = array('Html');
    var $options = array();
    function __construct($options = null) {
        parent::__construct();        
        $view = & ClassRegistry::getObject('view');        
    }
    function navigation($optlinks = "" , $total = 0, $current = 1, $per_page = 10) {
       // echo $this->Html->link(__l('Recent Deals'), $optlinks, array('title' => __l('Recent Deals')));exit;
        $num_page = ceil($total/$per_page);
        if ($num_page == 1)
            return "";
        $html = '<div class="paging">';
        if($current > 1) {
            $url = array_merge(array('page' => $current - 1), $optlinks);
            $html .= $this->Html->link('&laquo; ' . __l('Prev'),$url,array('class'=>'prev','escape'=>false));
        }
        if($num_page < 6) {
            //if only 5 pages -> display all
            for($i = 1; $i <= $num_page; $i ++) {
                if ( $i != $current)
                {
                    $url = array_merge(array('page' => $i), $optlinks);
                    $html.= '<span>'.$this->Html->link($i,$url).'</span>';                    
                }
                else
                {
                    $html.= '<span class="current">'.$i.'</span>';                    
                }
            }
        }
        else {
            //only display 5 page closest
            if($current < 3) {
                //display from 1 to 5
                $start = 1;
            }
            elseif ($current > $num_page - 2) {
                //display from numpage - 4 to numpage
                $start = $num_page - 4;
            }
            else {
                //display from current - 2 to current + 2
                $start = $current - 2;
            }
            for($i = $start; $i < $start+5; $i ++) {
                if ( $i != $current)
                {
                    $url = array_merge(array('page' => $i), $optlinks);
                    $html.= '<span>'.$this->Html->link($i,$url).'</span>';                     
                }
                else
                    $html.= '<span class="current">'.$i.'</span>';
            }
        }
        //khuc cuoi
        if ($current < $num_page) {
            $next = $current + 1;
            $url = array_merge(array('page' => $next), $optlinks);
            $html .= $this->Html->link(__l('Next') . ' &raquo;',$url,array('class'=>'next','escape'=>false));
        }
        $html   .= '</div>';
        return $html;
    }
    
}
?>